<?php

namespace app\teacher\controller;

use app\teacher\utils\User as AdminUser;
use think\Db;

class Member extends Base
{
    // 学生列表
    public function index()
    {
        $page = request()->param('page', 1, 'intval');
        $limit = request()->param('limit', 20, 'intval');
        $student_no = request()->param('student_no', '');
        $realname = request()->param('realname', '');
        $zhuanye = request()->param('zhuanye', '');
        $class = request()->param('class', '');
        $grade = request()->param('grade', '');
        $house_id = request()->param('house_id', '');
        $sushe_id = request()->param('sushe_id', '');
        $where = [];
        $uid = AdminUser::getUid();
        $user_type = $this->user_type();
        if($user_type == 1){//辅导员
            $where[] = ['fdy', '=', $uid];
        }else{
            $xueyuan_id = Db::name('yuan')->where('yuanzhang',$uid)->value('id');
            $where[] = ['xueyuan','=',$xueyuan_id];
        }
        if ($student_no) {
            $where[] = ['student_no', 'like', '%' . $student_no . '%'];
        }
        if ($realname) {
            $where[] = ['realname', 'like', '%' . $realname . '%'];
        }
        if ($zhuanye) {
            $where[] = ['zhuanye', 'like', '%' . $zhuanye . '%'];
        }
        if ($class) {
            $where[] = ['class', 'like', '%' . $class . '%'];
        }
        if ($grade) {
            $where[] = ['grade', 'like', '%' . $grade . '%'];
        }
        if ($house_id) {
            $where[] = ['house_id', '=', $house_id];
        }
        if ($sushe_id) {
            $where[] = ['sushe_id', '=', $sushe_id];
        }
        $list = model('member')->where($where)->page($page, $limit)->field('uid,realname,student_no,zhuanye,class,grade,mobile,house_id,sushe_id')->select()->each(function ($item) {
            $item['class'] = Db::name('banji')->where('id',$item['class'])->value('title');
            $item['grade'] = model('member')::GRADE[$item['grade']];
            $house = model('House')->where('id',$item['house_id'])->value('title');
            $room = model('Room')->where('id',$item['sushe_id'])->value('title');
            $item['house_room'] = $house.$room;
        });
        $total = model('member')->where($where)->count();
        $house = model('House')->column('title','id');
        $grade = model('member')::GRADE;
        $data = [
            'list' => $list,
            'total' => $total,
            'house' => $house,
            'grade' => $grade
        ];
        return $this->ok($data);
    }

    //学生详情
    public function detail(){
        $id = request()->param('id',0,'intval');
        $uid = AdminUser::getUid();
        if(!$id){
            return $this->fail('参数错误');
        }
        $detail = model('Member')->where('fdy',$uid)->where('uid',$id)->field('uid,realname,student_no,zhuanye,class,grade,mobile,house_id,sushe_id,head_img,jinji_chat,home_mobile')->find();
        if(empty($detail)){
            return $this->fail('该学生信息不存在');
        }
        $detail['head_img'] = !empty($detail['head_img']) ? model('File')->getPath($detail['head_img']) : '';
        $house = model('House')->where('id',$detail['house_id'])->value('title');
        $room = model('Room')->where('id',$detail['sushe_id'])->value('title');
        $detail['house_room'] = $house.$room;
        $detail['grade'] = model('member')::GRADE[$detail['grade']];
        $detail['class'] = Db::name('banji')->where('id',$detail['class'])->value('title');
        return $this->ok($detail);
    }
}
